﻿Imports Abyx.SVCMS.Library
Imports Abyx.SVCMS.Library.DB
Imports Abyx.WindowsExtendedControls


Public Class SVC_ServiceOrder_Form
    Implements ICarfDataEditingChildWindow

#Region "Declarations"
    Dim iMainDataObject As ServiceOrderType = Nothing
    Dim iFormActionOnData As FormActionOnDataEnum

    Dim iCustomersDataObject As CustomerTypeCollection = Nothing
    Dim iSelectedCustomerDataObject As CustomerType = Nothing

    Dim iEquipmentCatalogDataObject As CatalogItemTypeCollection = Nothing
    Dim iSelectedCatalogItemDataObject As CatalogItemType = Nothing
#End Region

#Region "ICARF Implementations"
    Public Function GetFormInstanceForDataObject(DataObject As Object) As System.Windows.Forms.Form Implements ICarfDataEditingChildWindow.GetFormInstanceForDataObject

        If (DataObject IsNot Nothing) _
            AndAlso (TypeOf DataObject Is ServiceOrderType) Then
            If Me.iMainDataObject = DataObject Then
                Return Me
            End If
        End If
        Return Nothing

    End Function
    Public Sub OrderRefreshRelatedDataControls(Optional DataItem As Object = Nothing, _
                                               Optional ActionOnDataItem As FormActionOnDataEnum = FormActionOnDataEnum.None) Implements ICarfDataEditingChildWindow.OrderRefreshRelatedDataControls

        Dim main As ICarfParentWindow = Me.MdiParent
        main.OrderRefreshRelatedDataControls(Me, _
                                             RelatedControlDataCategoriesEnum.SOs, _
                                             DataItem, _
                                             ActionOnDataItem)

    End Sub
    Public Sub ProcessRefreshRelatedDataControls(sender As System.Windows.Forms.Form, _
                                                 DataTypeCategory As RelatedControlDataCategoriesEnum, _
                                                 Optional DataItem As Object = Nothing, _
                                                 Optional ActionOnDataItem As FormActionOnDataEnum = FormActionOnDataEnum.None) Implements ICarfDataEditingChildWindow.ProcessRefreshRelatedDataControls
        'nothing to do here
    End Sub
    Public Sub InitializeThisFormAndControlSettings(Optional OrderUpdateToFile As Boolean = False) Implements ICarfDataEditingChildWindow.InitializeThisFormAndControlSettings

        'init general settings
        AppSettings.InitFormSettings(Me, OrderUpdateToFile)

        'init customers data

    End Sub
    Public Sub Refresh_ControlsWithDataObject() Implements ICarfDataEditingChildWindow.Refresh_ControlsWithDataObject
        With Me.iMainDataObject
            Me.ServiceOrderId_TextBox.Text = .ServiceOrderId.ToString
            Me.OrderNumber_TextBox.Text = .OrderNumber
            Me.EstimatedAmount_NumericUpDown.Value = .EstimatedPrice

            Me.EstimatedDate_DateTimePicker.Value = .EstimatedDate
            Me.OrderDate_DateTimePicker.Value = .OrderReceivedDate
            Me.CompletedDate_NDateTimePicker.Value = .OrderCompletedDate
            Me.ActualAmount_NumericUpDown.Value = .ActualPrice

            'select customer
            Dim scs As IEnumerable(Of CustomerType) = From sc As CustomerType In Me.iCustomersDataObject _
                                                      Where sc.CustomerId.Equals(.CustomerId) _
                                                      Select sc
            If (scs IsNot Nothing) AndAlso (scs.Count > 0) Then
                Me.iSelectedCustomerDataObject = scs(0)
                Refresh_CustomerLabels()
            Else
                Me.iSelectedCustomerDataObject = Nothing
            End If

            'select service equipment
            Dim ses As IEnumerable(Of CatalogItemType) = From se As CatalogItemType In Me.iEquipmentCatalogDataObject _
                                                          Where se.EquipmentId.Equals(.ServiceEquipmentId) _
                                                          Select se
            If (ses IsNot Nothing) AndAlso (ses.Count > 0) Then
                Me.iSelectedCatalogItemDataObject = ses(0)
                Refresh_ServiceEquipmentLabels()
            Else
                Me.iSelectedCatalogItemDataObject = Nothing
            End If

        End With
    End Sub
    Public Sub Refresh_DataObjectWithControls() Implements ICarfDataEditingChildWindow.Refresh_DataObjectWithControls

    End Sub
#End Region

#Region "Constructors/Destructors"
    Public Overloads Sub Show(FormActionOnData As FormActionOnDataEnum, _
                              Optional DataObject As ServiceOrderType = Nothing)

        If DataObject IsNot Nothing Then
            Me.iMainDataObject = DataObject
            Me.iFormActionOnData = FormActionOnDataEnum.UpdateRecord
        Else
            Me.iMainDataObject = New ServiceOrderType(Guid.NewGuid)
            Me.iFormActionOnData = FormActionOnDataEnum.NewRecord
        End If
        MyBase.Show()
        Me.Refresh_ControlsWithDataObject()

    End Sub
    Private Sub STK_Grn_Form_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        Me.InitializeThisFormAndControlSettings()
        Refersh_CustomersDataObject()
        Refresh_EquipmentCatalogDataObject()

    End Sub
    Private Sub STK_Grn_Form_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

        Me.InitializeThisFormAndControlSettings(True)

    End Sub
#End Region

#Region "Events"
    'Customers SearchBox
    Private Sub Customer_SearchBox_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles Customer_SearchBox.SelectedIndexChanged

        If (Customer_SearchBox.SelectedItem IsNot Nothing) _
            AndAlso (TypeOf Customer_SearchBox.SelectedItem Is DetailListBoxItem) Then
            Dim item As DetailListBoxItem = Customer_SearchBox.SelectedItem
            If (item.DataObject IsNot Nothing) AndAlso (TypeOf item.DataObject Is CustomerType) Then
                Me.iSelectedCustomerDataObject = item.DataObject
            End If
        End If
        Customer_SearchBox.SelectedItem = Nothing
        Refresh_CustomerLabels()

    End Sub
    Private Sub Customer_SearchBox_TextChanged(sender As System.Object, e As System.EventArgs) Handles Customer_SearchBox.TextChanged

        Dim sitems As Collections.Generic.IEnumerable(Of CustomerType) = Nothing
        If Customer_SearchBox.Text <> "" Then
            sitems = From sitem As CustomerType In Me.iCustomersDataObject _
                         Where (sitem.CustomerNo & _
                                sitem.IdNo & _
                                sitem.Name & _
                                sitem.Address & _
                                sitem.Mob).ToLower.Contains(Customer_SearchBox.Text.ToLower)
                            Select sitem _
                            Order By sitem.CustomerNo
        Else
            sitems = From sitem As CustomerType In Me.iCustomersDataObject _
                         Select sitem _
                         Order By sitem.CustomerNo
        End If
        Customer_SearchBox.PopupListBox.Items.Clear()
        If (sitems IsNot Nothing) AndAlso (sitems.Count > 0) Then
            For i As Integer = 0 To sitems.Count - 1
                Dim customer As CustomerType = sitems(i)
                Customer_SearchBox.PopupListBox.Items.Add(customer.ToDetailsListBoxItem)
            Next
        End If

    End Sub
    'Generate OrderNumber ImageButton
    Private Sub GenerateOrderNumber_ImageButton_Click(sender As System.Object, e As System.EventArgs) Handles GenerateOrderNumber_ImageButton.Click

        Dim sysman As New SysManager(AppSettings.LoggedUser)
        Me.iMainDataObject.OrderNumber = sysman.Get_SequenceNo(SequenceNumeringNamesEnum.ServiceOrderNumberSequence)
        Me.OrderNumber_TextBox.Text = Me.iMainDataObject.OrderNumber

    End Sub
    'OrderNumber TextBox
    Private Sub OrderNumber_TextBox_TextChanged(sender As System.Object, e As System.EventArgs) Handles OrderNumber_TextBox.TextChanged
        Me.Text = "SO-" & Me.OrderNumber_TextBox.Text
    End Sub
    'Service Equipment SearchBox
    Private Sub ServiceEquipment_SearchBox_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ServiceEquipment_SearchBox.SelectedIndexChanged

        If (ServiceEquipment_SearchBox.SelectedItem IsNot Nothing) _
            AndAlso (TypeOf ServiceEquipment_SearchBox.SelectedItem Is DetailListBoxItem) Then
            Dim item As DetailListBoxItem = ServiceEquipment_SearchBox.SelectedItem
            If (item.DataObject IsNot Nothing) AndAlso (TypeOf item.DataObject Is CatalogItemType) Then
                Me.iSelectedCatalogItemDataObject = item.DataObject
            End If
        End If
        ServiceEquipment_SearchBox.SelectedItem = Nothing
        Refresh_ServiceEquipmentLabels()

    End Sub
    Private Sub ServiceEquipment_SearchBox_TextChanged(sender As Object, e As System.EventArgs) Handles ServiceEquipment_SearchBox.TextChanged

        Dim sitems As Collections.Generic.IEnumerable(Of CatalogItemType) = Nothing
        If ServiceEquipment_SearchBox.Text <> "" Then
            sitems = From sitem As CatalogItemType In Me.iEquipmentCatalogDataObject _
                         Where (sitem.ProductModelNumber & _
                                sitem.Name & _
                                sitem.ManufacturerName & _
                                sitem.Description).ToLower.Contains(Customer_SearchBox.Text.ToLower)
                            Select sitem _
                            Order By sitem.ProductModelNumber
        Else
            sitems = From sitem As CatalogItemType In Me.iEquipmentCatalogDataObject _
                         Select sitem _
                         Order By sitem.ProductModelNumber
        End If
        ServiceEquipment_SearchBox.PopupListBox.Items.Clear()
        If (sitems IsNot Nothing) AndAlso (sitems.Count > 0) Then
            For i As Integer = 0 To sitems.Count - 1
                Dim item As CatalogItemType = sitems(i)
                ServiceEquipment_SearchBox.PopupListBox.Items.Add(item.ToDetailsListBoxItem)
            Next
        End If

    End Sub

#End Region

#Region "Private Procedures"
    Private Sub Refersh_CustomersDataObject()
        Dim svm As New SvcManager(AppSettings.LoggedUser)
        Me.iCustomersDataObject = svm.Get_Customers_Search_COL("")
    End Sub
    Private Sub Refresh_EquipmentCatalogDataObject()

        Dim sqc As New SqcManager(AppSettings.LoggedUser)
        Me.iEquipmentCatalogDataObject = sqc.Get_CatalogItems_All

    End Sub
    Private Sub Refresh_CustomerLabels()

        If Me.iSelectedCustomerDataObject IsNot Nothing Then
            Me.CustomerNo_RoundCornerLabel.Text = "Customer No.: " & Me.iSelectedCustomerDataObject.CustomerNo & vbNewLine & _
                                                  "Id          : " & Me.iSelectedCustomerDataObject.IdNo & vbNewLine & _
                                                  "Name        : " & Me.iSelectedCustomerDataObject.Name & vbNewLine & _
                                                  "Address     : " & Me.iSelectedCustomerDataObject.Address & vbNewLine & _
                                                  "Contact     : " & Me.iSelectedCustomerDataObject.Mob
        Else
            Me.CustomerNo_RoundCornerLabel.Text = ""
        End If

    End Sub
    Private Sub Refresh_ServiceEquipmentLabels()

        If Me.iSelectedCatalogItemDataObject IsNot Nothing Then
            Me.ServiceEquipment_RoundCornerLabel.Text = Me.iSelectedCatalogItemDataObject.ProductModelNumber & vbNewLine & _
                                                        Me.iSelectedCatalogItemDataObject.Name & vbNewLine & _
                                                        Me.iSelectedCatalogItemDataObject.Description
        Else
            Me.ServiceEquipment_RoundCornerLabel.Text = ""
        End If

    End Sub
#End Region






End Class
